﻿using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

using Microsoft.Win32;

using Aulac.Srv.Db.Connect; using Aulac.Srv.Db.Common;
using System.Collections.Generic;
using Aulac.Global.Enums; using Aulac.Global.Data; using Aulac.Global.Common; using Aulac.Global.Xml;
using Aulac.Srv.Db.Data.Catalogs;
using Aulac.Rsc.Bss;
using Aulac.Rsc.Bss.Tags;
using Aulac.Srv.Data.XmlQuery;



namespace Aulac.Srv.Db.Data.Ems.Items
{
	/// <summary>
	/// Summary description for MedicineItem.
	/// </summary>
    public partial class MdnItems : Aulac.Srv.Db.Data.Ems.Document.DbEmsDataDocumentMangager
	{
        public MdnItems(IDbAulacConnect p_Db)
            : base(p_Db)
        {
        }
        List<string> mList = new List<string>(new string[] { tagItems.Items.ItemPrkID, tagItems.Items.ItemID, tagItems.Items.ItemName });

        public XmlDataDocument GetListItem(XmlDataDocument xmlData)
        {
            try
            {
                string[] sArrGrpPrkID = XmlProcess.GetValueParam(xmlData, tagItems.MdnGrpItems.ItemGrpPrkID).Split(',');
                string sItemGrpPrkID = "";
                for (int i = 0; i < sArrGrpPrkID.Length; i++)
                    sItemGrpPrkID += string.Format("{0}{1}{0},", "'", sArrGrpPrkID[i]);
                sItemGrpPrkID = sItemGrpPrkID.TrimEnd(',');


                string sArrItemPrkID = XmlProcess.GetValueParam(xmlData, tagItems.Items.ItemID);
                XmlDataDocument xmlDoc = CreateXmlDocument("", "");
                string sqlQuery = string.Format("SELECT GI.ItemGrpID, I.ItemID, I.ItemPrkID, I.ItemName, I.Unit, ");
                sqlQuery += string.Format(" GI.ItemGrpPrkID");
                sqlQuery += string.Format(" FROM {0} GI", TableNames.Dm_MdnGrpItems);
                sqlQuery += string.Format(" RIGHT OUTER JOIN {0} I ON GI.ItemGrpID = I.ItemGrpID", TableNames.Dm_MdnItems);
                sqlQuery += string.Format(" WHERE (GI.{0} IN ({1}))", tagItems.MdnGrpItems.ItemGrpPrkID, sItemGrpPrkID);
                SrvExecXmlQueryData query = new SrvExecXmlQueryData(DbDataProvider);
                query.SetVchLinesElm(QueryCmdTypes.SelectData);
                query.ObjQueryVchLines.SqlQueryString = sqlQuery;
                return query.ExecureQuery();
                
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
  
        //private  string StringFilterGrp(string pPerFix, string[] pCollectionMdnGrp, bool pIsGetAllChild)
        //{
        //    if (pCollectionMdnGrp == null || pCollectionMdnGrp.Length == 0)
        //        return "";

        //    string sPerFix = Aulac.Global.Common.ValueHelper.isEmpty(pPerFix) ? "" : string.Format("{0}.", pPerFix); 
        //    string str = "";
        //    foreach(string s in pCollectionMdnGrp)
        //        str += string.Format("{0}{1} LIKE N'{2}%' OR ", sPerFix, tagItems.ItemGrps.ItemGrpID, s);
        //    str = StringHelper.RemoveEnd(str, 3);
            
        //    if (pIsGetAllChild == false)
        //        str = str.Replace("%", "");
        //    return str;
        //}

        //private string StringFilterGrp(string pPerFix, string pStringOfCollectionMdnGrp, bool pIsGetAllChild)
        //{
        //    return StringFilterGrp(pPerFix, Aulac.Global.Common.StringHelper.BuildArrayFromString( pStringOfCollectionMdnGrp, ","), pIsGetAllChild);
        //}
        //public XmlDataDocument RptListItemByGrp(XmlDataDocument xmlDoc)
        //{
        //    DataTable lTable = new DataTable();
        //    GetListMdnItem(xmlDoc, ref lTable);
        //    XmlDataDocument ouDoc = CreateXmlDocument("", "");
        //    XmlElement elines = XmlProcess.AddVchLinesElm(ref ouDoc);
        //    XmlElement eGrps = XmlProcess.AddChildNode(ref elines, tagCtrl.GROUPS);
        //    XmlElement eGrp = null;
        //    string lGrp = "";
        //    foreach (DataRow row in lTable.Rows)
        //    {
        //        string grp = row[tagItems.ItemGrps.ItemGrpID] as string;

        //        if (!lGrp.Equals(grp))
        //        {
        //            eGrp = eGrps.SelectSingleNode(tagCtrl.GROUPS + "[@" + tagItems.ItemGrps.ItemGrpID + "= '" + grp + "']") as XmlElement;
        //            if (eGrp == null)
        //            {
        //                eGrp = XmlProcess.AddChildNode(ref eGrps, tagCtrl.GROUPS);
        //                eGrp.SetAttribute(tagItems.ItemGrps.ItemGrpID, grp);
        //                eGrp.SetAttribute(tagItems.ItemGrps.ItemGrpName, row[tagItems.ItemGrps.ItemGrpName] as string);
        //            }
        //            lGrp = grp;
        //        }
        //        XmlElement eline = XmlProcess.AddChildNode(ref eGrp, tagCtrl.LINE);
        //        eline.SetAttribute(tagItems.Items.ItemID, row[tagItems.Items.ItemID] as string);
        //        eline.SetAttribute(tagItems.Items.ItemName, row[tagItems.Items.ItemName] as string);
        //        eline.SetAttribute(tagItems.Items.Unit, row[tagItems.Items.Unit] as string);
        //    }
        //    return ouDoc;
        //}

        //public void GetListMdnItem(XmlDataDocument xmlParam, ref DataTable pTable)
        //{
        //    /* Update Code: 01/22/2011 by Thangnc */
        //     GetListMdnItemDb(xmlParam, ref pTable);
        //}

        //public DataRow GetMdnItem(string pItemPrkID)
        //{
        //    string str = Aulac.Global.Common.StringHelper.ConvertArrayToString(",i.", mList.ToArray());
        //    sqlQuery = string.Format("SELECT i.{0}  FROM {1}  i ", str, TableNames.Dm_MdnItems);
        //    sqlQuery += string.Format(" WHERE (? = '' OR ? IS NULL OR i.{0} = ? )", tagItems.Items.ItemPrkID);

        //    DataTable tb = null;
        //        DbDataProvider.ExecuteReaderDataToTable(mDbCmd, sqlQuery, ref tb, pItemPrkID, pItemPrkID, pItemPrkID);
        //    if (tb != null && tb.Rows.Count > 0)
        //        return tb.Rows[0];
        //    return null;
        //}
	}
}
